﻿using System;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using Relocation.Data;
using Relocation.Com.Tools;
using Relocation.Service;
using Relocation.Com;
using Relocation.Base;
using Relocation.Base.UI;

namespace Relocation.Forms
{
	public partial class ProjectPic : BaseForm
	{
		
		
		Project _Project;
		Relocation.Data.File _PicFiles;

		public ProjectPic(Session session,Project project):base(session)
		{
			InitializeComponent();		
			this._Project = project;
		}

		private void ButtonUpload_Click(object sender, EventArgs e)
		{
			if (this.TextBoxFilePath.Tag == null)
			{
				MyMessagebox.Show("请先上传图片");
				return;
			}
			try
			{
				string FullFileName = (string)this.TextBoxFilePath.Tag;
				string Name = Path.GetFileName(FullFileName);

				if (FullFileName == "" || !System.IO.File.Exists(FullFileName))
					return;

				if (this._PicFiles == null)
				{
					this._PicFiles = new Relocation.Data.File();
					this._PicFiles.file_id = DataModel.GenerateID(this._PicFiles);
					this._PicFiles.Project = this._Project;
					this._PicFiles.file_type = 1;
					string NewFileName = Tools.SaveFileToDocs(FullFileName);
					this._PicFiles.path = NewFileName;
				}
				else
				{
					//修改记录时，如果重新选择了文件
					string oldFileName = this._PicFiles.path;
					if (FullFileName != oldFileName)
					{
						Tools.DeleteFile_Directory(oldFileName);
						string NewFileName = Tools.SaveFileToDocs(FullFileName);
						this._PicFiles.path = NewFileName;
					}
				}
				this._PicFiles.file_name = Name;
				this._PicFiles.remark = this.TextBoxRemark.Text;
				this.Session.DataModel.Save(this._PicFiles);
				MyMessagebox.Show("保存成功");
				this.DialogResult = DialogResult.OK;
				this.Close();
			}
			catch (Exception ex)
			{				
				Log.Error(ex.GetInnerExceptionMessage());
				MyMessagebox.Error("操作失败！");
			}
		}

		private void ButtonSelectFile_Click(object sender, EventArgs e)
		{
			OpenFileDialog OpenFileDialog = new OpenFileDialog();
			OpenFileDialog.Filter = "图片或DWG文件 (*.jpg,*.jpeg,*.bmp,*.png,*.gif,*.dwg,*.dfx,*.dwf)|*.jpg;*.jpeg;*.bmp;*.png;*.gif;*.dwg;*.dfx;*.dwf|所有文件(*.*)|*.*";
			OpenFileDialog.FilterIndex = 0;
			OpenFileDialog.RestoreDirectory = false;
			OpenFileDialog.Multiselect = false;			
			if (DialogResult.OK.Equals(OpenFileDialog.ShowDialog(this)))
				return;
			string FullFileName = OpenFileDialog.FileName;
			string FileName = Path.GetFileName(FullFileName);
			this.TextBoxFilePath.Text = FileName;
			this.TextBoxFilePath.Tag = FullFileName;
			this.TextBoxRemark.Text = "";
			try
			{
				Tools.ShowImage(PictureBox1, FullFileName);
			}
			catch (Exception ex)
			{
				Log.Error(ex.Message);
				MyMessagebox.Show(ex.Message);
			}
		}

		private void ProjectPic_Load(object sender, EventArgs e)
		{
			try
			{
				if (this.Session == null)
					return;
				this._Project.Files.Load();
				this._PicFiles = this._Project.Files.FirstOrDefault(t => t.file_type == 1);
				if (this._PicFiles == null)
					return;
				this.Session.DataModel.Refresh(System.Data.Objects.RefreshMode.StoreWins, this._PicFiles);
				Tools.ShowImage(PictureBox1, this._PicFiles.path);
				this.TextBoxFilePath.Text = this._PicFiles.file_name;
				this.TextBoxFilePath.Tag = this._PicFiles.path;
				this.TextBoxRemark.Text = this._PicFiles.remark;
			}
			catch (Exception ex)
			{
				Log.Error(ex.InnerException == null);
			}

		}
		/// <summary>
		/// 删除文件
		/// </summary>		
		private void ButtonDelete_Click(object sender, EventArgs e)
		{
			try
			{
				this.PictureBox1.Image = null;
				if (this._PicFiles != null)
				{
					Tools.DeleteFile_Directory(this._PicFiles.path);
					this.Session.DataModel.Delete(this._PicFiles);
				}
				this._PicFiles = null;
				this.TextBoxFilePath.Text = "";
				this.TextBoxFilePath.Tag = null;
				this.TextBoxRemark.Text = "";
				MyMessagebox.Show("删除成功！");
			}
			catch (Exception ex)
			{			
				Log.Error(ex.GetInnerExceptionMessage());
				MyMessagebox.Error("操作失败！");
			}
		}

		/// <summary>
		/// 打开文件
		/// </summary>		
		private void ButtonOpen_Click(object sender, EventArgs e)
		{
			if (this.TextBoxFilePath.Tag == null)
			{
				MyMessagebox.Show("请先上传图片");
				return;
			}
			try
			{
				string fileName = (string)this.TextBoxFilePath.Tag;
				Tools.OpenFile(fileName);
			}
			catch (Exception ex)
			{				
				Log.Error(ex.GetInnerExceptionMessage());
				MyMessagebox.Error("操作失败！");
			}
		}
	}
}
